//
//  JXHPhotoBrowserCell.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/11/14.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit
import SDWebImage

protocol JXHPhotoBrowserCellDelegate {
    
    func imageTapClick()
}

class JXHPhotoBrowserCell: UICollectionViewCell {
    
    var picURL : URL? {
        didSet {
         setupContent(picURL: picURL)
        }
    }
    var delegate : JXHPhotoBrowserCellDelegate?
    
    lazy var scrollView : UIScrollView = UIScrollView()
    lazy var imageView : UIImageView = UIImageView()
    lazy var progressView : JXHCostomProgressView = JXHCostomProgressView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension JXHPhotoBrowserCell {
    func setupUI() {
    
        contentView.addSubview(scrollView)
        contentView.addSubview(progressView)
        scrollView.addSubview(imageView)
        scrollView.frame = contentView.bounds
        scrollView.frame.size.width -= 20 //目的是留出图片间距
        progressView.bounds = CGRect.init(x: 0, y: 0, width: 50, height: 50)
        progressView.center = CGPoint.init(x: UIScreen.main.bounds.width * 0.5, y: UIScreen.main.bounds.height * 0.5)
        progressView.isHidden = true
        progressView.layer.cornerRadius = 25
        progressView.layer.borderColor = UIColor.white.cgColor
        progressView.layer.masksToBounds = true
        progressView.layer.borderWidth = 2
        progressView.backgroundColor = UIColor.black.withAlphaComponent(0.1)
        
        //添加手势在图片上
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(JXHPhotoBrowserCell.imageTapGestureClick))
        imageView.addGestureRecognizer(tap)
        
    }
}

// MARK: - 手势事件监听
extension JXHPhotoBrowserCell {

   @objc fileprivate func imageTapGestureClick() {
        delegate?.imageTapClick()
    }
}


extension JXHPhotoBrowserCell {
    func setupContent(picURL : URL?) {
        guard let picURL = picURL else {
            return
        }
        //取出Image对象
        guard let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: picURL.absoluteString) else {
            return
        }
        let x : CGFloat = 0
        let width = UIScreen.main.bounds.width
        let height = width / image.size.width * image.size.height
        var y : CGFloat = 0
        
        if height > UIScreen.main.bounds.height {//大图
            y = 0
        }else{
            y = (UIScreen.main.bounds.height - height) * 0.5
        }
        imageView.frame = CGRect.init(x: x, y: y, width: width, height: height)
        progressView.isHidden = false
        imageView.sd_setImage(with: getBigURL(smallURL: picURL), placeholderImage: image, options: [], progress: { (current, total) in
            
            self.progressView.progress = CGFloat(current) / CGFloat(total)
            }) { (_, _, _, _) in
            self.progressView.isHidden = true
        }
        //设置contentSize
        scrollView.contentSize = CGSize.init(width: 0, height: height)
        
        
    }
    
    func getBigURL(smallURL : URL) -> URL {
        
        let smallURLString = smallURL.absoluteString
        let bigURLString = smallURLString.replacingOccurrences(of: "thumbnail", with: "large")
        return URL.init(string: bigURLString)!
        
    }
}

